In [8]:
%matplotlib notebook
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
figsize = (8,4)
# Define a bunch of transfer functions
G1 = signal.TransferFunction([10], [1, 10])
G2 = signal.TransferFunction([1, 100], [1, 10])
G3 = signal.TransferFunction([1, 20], [1, 58, 407, 350]) # this one we'll draw in class
G4 = signal.TransferFunction([1, 2*0.2*10, 100],[1])
For each transfer function we generate the frequency response.
This outputs the frequency, magnitude (decibels) and phase (degrees).
Then we simply plot it on a semilog axes.
In [9]:
w1, mag1, phase1 = signal.bode(G1, np.logspace(-2,4))
# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w1,mag1)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)
axarr[1].semilogx(w1, phase1)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)
plt.show()
In [10]:
w2, mag2, phase2 = signal.bode(G2, np.logspace(-2,4))
# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w2,mag2)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)
axarr[1].semilogx(w2, phase2)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)
plt.show()
In [11]:
w4, mag4, phase4 = signal.bode(G4, np.logspace(-2,4))
# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w4,mag4)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)
axarr[1].semilogx(w4, phase4)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)
plt.show()
In [12]:
w3, mag3, phase3 = signal.bode(G3, np.logspace(-2,4))
# Problem 2
fig, axarr=plt.subplots(1,2, figsize=figsize)
axarr[0].semilogx(w3,mag3)
axarr[0].set_title('Magnitude')
axarr[0].set_xlabel('Frequency (rad/sec)')
axarr[0].set_ylabel('Magnitude (db)')
axarr[0].grid(True)
axarr[1].semilogx(w3, phase3)
axarr[1].set_title('Phase')
axarr[1].set_xlabel('Frequency (rad/sec)')
axarr[1].set_ylabel('Phase (deg)')
axarr[1].grid(True)
plt.show()
In [13]:
mag3
Out[13]: